<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\ActivityPhoto;
use app\common\model\Area;
use app\common\model\Evaluate;
use app\common\model\HistorySearch;
use app\common\model\Order as OrderModel;
use app\common\model\ProductCate;
use app\common\model\User;
use app\common\tool\Result;
use GuzzleHttp\HandlerStack;
use think\facade\Cache;
use think\facade\View;
use think\response\Json;
use app\common\model\Activity as ActivityModel;

class Activity extends Base
{

    /**
     * get: 活动列表
     * path: getList
     * method: getList
     */
    public function getList(): \think\Response
    {
        $cate = ActivityModel::where(['status' => 1, 'is_del' => 0])->order('sort','asc')->order('create_time','desc')->field('id,title,price,images')->select();
        if (isset($cate)) {
            return Result::success('获取成功', $cate);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 活动发布
     * path: release
     * method: release
     * param: a_id - {string} 活动id
     * param: images - {string} 图片
     */
    public function release()
    {
        try {
            $param = $this->request->param();
            $param['u_id'] = $this->user->id;
            $param['create_time'] = time();
            ActivityPhoto::create($param);
            return Result::success('添加成功');
        } catch (\Exception $e) {
            return Result::success('添加失败', $e->getMessage());
}
    }



    /**
     * post: 申领
     * path: applyFor
     * method: applyFor
     * param: id - {string} 活动id
     * param: name - {string} 姓名
     * param: phone - {string} 电话
     * param: address - {string} 地址
     */
    public function applyFor(): \think\Response
    {
        $param = $this->request->post();
        if ($this->user->is_lock) {
            return Result::fail('账号已锁定');
        }
//        $num = \app\common\model\Address::where(['uid'=>$this->user->id,'default'=>1])->count();
//        if(!$num)
//        {
//            return Result::fail('请先设置默认地址');
//        }
        $submit = ActivityModel::submit($param, $this->user);
        if ($submit['code'] > 0) {
            return Result::success($submit['msg']);
        } else {
            return Result::fail($submit['msg']);
        }
    }





}

